<?php 
$req_path="./";
include_once($req_path."include/ini.php");
require_once($req_path."include/db.php");

class M_user extends DB{
	private $dbConn;
	private $rowCount="0";
	private $ID="0";
	private $sqlSelect ="select * from tb_user where 1=1";
	private $totalpage="0";
	function M_user(){
		parent::__construct();
	}
	function getText(){
		echo "TEST";
	}
	function getListPage(){
		global $list_page;
		return $list_page;
	}
	function checkBranch($bid=0){
		$sql="select b.Type from tb_branch b where b.ID=".$bid;
		
		$result = $this->ExecuteData($sql);
		//$row=$result->FetchRow();
		return $result->FetchRow();//1:header offie , 0 :branch
	}
	function getData($name=null, $position=null, $group=null, $branch=null,$sms="0" ,$pageStart=1){
		global $list_page;
		$sqlscript= "Select u.UserID, u.UserName , u.FirstName, u.LastName, p.PositionName Position "
				." , b.BranchName Branch , g.GroupName 'Group' ,u.Email, u.Telephone "
				." , CASE WHEN u.IsActived =1 THEN 'Actived' ELSE 'Disabled' END Status , u.SMS "
				." From tb_user u "
				." LEFT join tb_position p on p.ID=u.PositionID "
				." LEFT join tb_branch b on b.ID=u.BranchID "
				." LEFT join tb_usergroup g on g.GroupID=u.GroupID "
				." Where 1=1 ";
		
		if (isset($position) && $position!="0"){
			$sqlscript.= " and PositionID=". $position;
		}
		if (isset($name) && $name!=""){
			$sqlscript.= " and FirstName Like '%". $name."%'";
		}
		if (isset($sms) && $sms!="0"){
			$sqlscript.= " and SMS='".$sms."'";
		}
		if (isset($group) && $group !="0"){
			$sqlscript.= " and u.GroupID = ". $group."";
		}
		if (isset($branch) && $branch !="0"){
			$sqlscript.= " and BranchID = ". $branch."";
		}
		//Order 
		$sqlscript.= " order by  u.UserName asc " ;
		//Execute
		$result = $this->ExecuteData($sqlscript);
		//Get all rows
		$rows =$result->RecordCount();
		
		//Page limit
		$sqlscript.= " limit ". ($pageStart-1)*$list_page. ", " . $list_page;
		
		//paging
		$result = $this->ExecuteData($sqlscript);
		
		$totalpage=1;
		if ($rows>0){
			$rt = $rows%$list_page;
			
			if($rt!=0) {
				$totalpage = floor($rows/$list_page)+1;
			}
			else {
				$totalpage = floor($rows/$list_page);
			}
		}
		
		$this->rowCount=$rows;
		$this->totalpage=$totalpage;	
		
		return $result;
	}
	
	function getDataDetail($id=0){
		$sqlscript= "Select u.UserID, u.UserName, Password , u.FirstName, u.LastName, p.PositionName Position "
				." , b.BranchName Branch , g.GroupName 'Group' ,u.Email, u.Telephone ,u.SMS "
				." , CASE WHEN u.IsActived =1 THEN 'Actived' ELSE 'Disabled' END Status "
				." , PositionID, BranchID, u.GroupID "
				." From tb_user u "
				." LEFT join tb_position p on p.ID=u.PositionID "
				." LEFT join tb_branch b on b.ID=u.BranchID "
				." LEFT join tb_usergroup g on g.GroupID=u.GroupID "
				." Where 1=1 and UserID='".$id."'";
		$result = $this->ExecuteData($sqlscript);
		return $result->FetchRow();
	}
	function checkUserName($username){
		$sql="select * from tb_user where UserName='".$username."'";
		$result = $this->ExecuteData($sql);
		$rows=$result->RecordCount();
		$val=$rows > 0? "0":"1";
		return $val;
	}
	function getRowCount(){
		return $this->rowCount;
	}
	
	function getLastInsert(){
		return $this->ID;
	}
	
	function InsertData($val){
		if (isset($val)){
			$sqlInsert=$this->InsertSqlScript($this->sqlSelect, $val);
			$this->ExecuteData($sqlInsert);
			$this->ID = $this->getLastID();
			return $this->ID;
		}
	}
	function updateData($sql, $val){
		if(isset($val)){
			$sqlUpdate=$this->UpdateSqlScript($sql,$val);
			// return $sqlUpdate;
			return $this->ExecuteData($sqlUpdate);
		}
	}
	function delData($id){
		if(isset($id)){
			$sqlDelete = "delete from tb_user where UserID='".$id."'";
			$this->ExecuteData($sqlDelete);
			return "c";
		}
	}
	function getDataCBO($t="b"){
		if ($t=='b'){
			$sqlscript= "select ID as ID, BranchName as Code from tb_branch where IsActived=1 order by   BranchName asc ";
				$result = $this->ExecuteData($sqlscript);
				
				 while($row = $result->FetchRow()){
						$ar[$row["Code"]] = $row["ID"];
				 }
				 
				return $ar;
			}
		else if ($t=='g')
			$sqlscript= "select GroupID as ID, GroupName as Code from tb_usergroup where 1=1 and Status=1 ";
		else if ($t='p')	
			$sqlscript= "select ID as ID, PositionName as Code from tb_position where 1=1  and IsDeleted = 0 ";
				
		$result = $this->ExecuteData($sqlscript);
		
		 while($row = $result->FetchRow()){
			$ar[$row["ID"]] = $row["Code"];
		 }
		 
		return $ar;
	}
	function getTotalPage(){
		return $this->totalpage;
	}
}
?>